package xquery;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class XQContext {
	//All the string constants will be treated as text nodes of this document
	
	private Map<String,Document> currentDocs;
	private Document resultDoc;
	private Stack<HashMap<String,XPathData>> contextTable = null;

	
	public Map<String,Document> getCurrentDocs() {
		return currentDocs;
	}
	public void setCurrentDoc(Map<String,Document> currentDocs) {
		this.currentDocs = currentDocs;
	}
	public Document getResultDoc() {
		return resultDoc;
	}
	public void setResultDoc(Document resultDoc) {
		this.resultDoc = resultDoc;
	}
	

	public void put(String key,XPathData value)
	{
		HashMap<String,XPathData> scope = contextTable.peek();
		scope.put(key,value);
	}
	public XPathData get(String key)
	{
		XPathData value = null;
		HashMap<String, XPathData> scope = null;
		for(int i = contextTable.size()-1;i>=0;i--)
		{
			scope = contextTable.get(i);
			value = scope.get(key);
			if(value != null)
				break;
		}
		return value;
	}
	public void openContext()
	{
		HashMap<String, XPathData> scope = new HashMap<String, XPathData>();
		contextTable.push(scope);
	}
	public void closeContext()
	{
		contextTable.pop();
	}
	public XQContext()
	{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(false);
            DocumentBuilder db=null;
			try {
				db = dbf.newDocumentBuilder();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
				System.exit(1);
			}

            resultDoc = db.newDocument();
            currentDocs = new HashMap<String, Document>();
            contextTable = new Stack<HashMap<String,XPathData>>();
	}
}
